package user

import (
	"center-platform/internal/controller/response"
	"center-platform/internal/controller/v1/user/service"
	"github.com/gin-gonic/gin"
	"reflect"
)

func Login(c *gin.Context) {
	var userSer service.UserImpl = new(service.UserService)
	err, token := userSer.Login(c)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	response.ApiSuccess(c, token)
}

func Register(c *gin.Context) {
	var userSer service.UserImpl = new(service.UserService)
	err := userSer.Register(c)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	response.ApiSuccess(c, "添加成功")
}

func Info(c *gin.Context) {
	defer func() {
		if r := recover(); r != nil {
			response.ApiError(c, "服务器错误")
		}
	}()
	var userSer service.UserImpl = new(service.UserService)
	param, exists := c.Get("user")
	if exists == false {
		response.ApiError(c, "token已失效")
		return
	}
	id := reflect.ValueOf(param).FieldByName("UserId").Int()
	response.ApiSuccess(c, userSer.InfoById(int(id)))
}

func Update(c *gin.Context) {
	var userSer service.UserImpl = new(service.UserService)
	err := userSer.Update(c)
	if err != nil {
		response.ApiError(c, err.Error())
		return
	}
	response.ApiSuccess(c, "更新成功")
}
